import 'package:mvvm_provider/base/base_state.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_model.dart';
import 'package:mvvm_provider/page/home/data/repository/home_repository.dart';
import 'package:mvvm_provider/page/home/states/home_state.dart';
import 'package:mvvm_provider/until/app_util.dart';
import '../../../base/base_view_model.dart';
import '../../../config/handle_state.dart';
import '../../../model/response_model.dart';

class HomeViewModel extends BaseViewModel<HomeState> {
  HomeViewModel() : super(HomeState());

  /// 获取列表数据
  Future<void> getListData(bool isRefresh) async {
    int page = state.page;
    if (isRefresh) {
      page = 1;
    } else {
      page++;
    }
    ResponseModel? responseModel = await HomeRepository.getListData<CarDataModel>(page);
    state.netState = HandleState.handle(responseModel, successCode: 0);
    if (state.netState == NetState.dataSuccessState) {
      CarDataModel carDataModel = responseModel.data;
      if (page == 1) {
        state.dataList = carDataModel.feeds;
        if (AppUtil.isEmpty(state.dataList)) {
          state.netState = NetState.emptyDataState;
        }
      } else {
        state.dataList?.addAll(carDataModel.feeds ?? []);
      }
      state.page = page;
      refreshController.refreshCompleted();
      refreshController.loadComplete();

      /// 显示没有更多数据了
      if (AppUtil.isEmpty(carDataModel.feeds)) {
        refreshController.loadNoData();
      }
    }
    notifyListeners();
  }

  void changeIsLike() {
    state.isLike = !(state.isLike);
    notifyListeners();
  }
}
